//
//  MSDepartmentAttendanceController.m
//  觅尚
//
//  Created by No.8 on 2019/3/29.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSMyMonthController.h"
#import "MSRoomConsumOtionCell.h"
#import "MSMonthTongjiCell.h"
#import "MSDayCell.h"
#import "MSDayProgressView.h"
#import "CircleProgressView.h"
#import "MSDayStatisticsModel.h"
#import "MSMonthTongjiModel.h"
#import "MSDongjieController.h"
#import "MSLizhiListController.h"
#import "PickerView.h"

@interface MSMyMonthController ()<UITableViewDelegate,UITableViewDataSource,MSMonthTongjiCellDelegate>
{
    CustomNavigationBar *naviagtionBar;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic, strong) CircleProgressView *circleProgress;
@property (nonatomic, strong) NSTimer *timer;

/** staffCount*/
@property(nonatomic,strong)NSString *staffCount;

/** staffCount*/
@property(nonatomic,strong)NSMutableArray *departmentlistArray;


/** 遮盖层*/
@property(nonatomic,strong)UIView *backView;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView;


/** uid*/
@property(nonatomic,strong)NSString *uid;

/** uid*/
@property(nonatomic,strong)NSString *userName;
/** uid*/
@property(nonatomic,strong)NSString *attendacneState;
/** uid*/
@property(nonatomic,strong)NSString *avatarUrl;
/** <#name#>*/
@property(nonatomic,strong)UILabel *timeLabel;

/** <#name#>*/
@property(nonatomic,strong)NSString *timeStr;
@end

@implementation MSMyMonthController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.departmentlistArray = [NSMutableArray new];
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
- (void)MJChiBaoZiHeaderRefresh{
    //    [self.tableView.header beginRefreshing];
    //    [self loadShowproduct];
    [self ReloadData];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreatHeaderView];
    [self CreatView];
    
    [self tableView];
    
    
    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(progressTimer:) userInfo:nil repeats:YES];
}


- (void)ReloadData{
    [MBManager showLoading];
    [self.departmentlistArray removeAllObjects];
    NSDictionary * params = @{@"busid":[WYCAccountTool unarchivebelongBusid],@"queryTime":@"month",@"depid":[WYCAccountTool unarchivebelongDepid],@"roleId":[WYCAccountTool unarchivebelongRolid],@"dateTime":self.timeStr};
    [HTTPRequest POST:MSgetDepartmentAttendance params:params succeed:^(id responseObject) {
        WYCLog(@"=======%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
//            self.departmentlistArray = [MSMonthTongjiModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"departMonthAttendanceList"]];
            for (NSDictionary * dict in responseObject[@"data"][@"departMonthAttendanceList"]) {
                MSMonthTongjiModel * model = [MSMonthTongjiModel mj_objectWithKeyValues:dict];
                [self.departmentlistArray addObject:model];
                WYCLog(@"=======%@",model.userName);
            }
            
        }else{
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)CreatHeaderView{
    
    UIView * headerView  = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 150 * px - [self navBarBottom] - 120 * px - self.tabBarController.tabBar.height, ScreenWidth,120 * px)];
    headerView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:headerView];
    
    
    /** 冻结*/
    UIButton * dongjieButton = [[UIButton alloc] initWithFrame:CGRectMake(50 * px, 0, headerView.width / 2 - 100 * px, 100 * px)];
    [dongjieButton setTitle:@"冻结列表" forState:UIControlStateNormal];
    dongjieButton.layer.cornerRadius = 50 * px;
    dongjieButton.layer.masksToBounds = YES;
    dongjieButton.layer.borderColor = FontColor.CGColor;
    dongjieButton.layer.borderWidth = 1;
    dongjieButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [dongjieButton  setTitleColor:FontColor forState:UIControlStateNormal];
    [dongjieButton  addTarget:self action:@selector(dongjieButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [headerView  addSubview:dongjieButton];
    
    
    /** 离职*/
    UIButton * lizhiButton = [[UIButton alloc] initWithFrame:CGRectMake(headerView.width / 2 + 50 * px, dongjieButton.y, dongjieButton.width, dongjieButton.height)];
    [lizhiButton setTitle:@"离职列表" forState:UIControlStateNormal];
    lizhiButton.layer.cornerRadius = 50 * px;
    lizhiButton.layer.masksToBounds = YES;
    [lizhiButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
    lizhiButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [lizhiButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [lizhiButton  addTarget:self action:@selector(lizhiButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [headerView  addSubview:lizhiButton];
}
- (void)CreatView{
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 330 * px)];
    HeaderView.backgroundColor = backgroundcolor;
    [self.view addSubview:HeaderView];
    
    UIView * timeView = [[UIView alloc] initWithFrame:CGRectMake(50 * px, 50 * px, ScreenWidth * 0.6, 120 * px)];
    timeView.layer.cornerRadius = 10 * px;
    timeView.layer.borderWidth = 1;
    timeView.layer.borderColor = WYCRGBColor(33, 46, 45).CGColor;
    [HeaderView addSubview:timeView];
    
    
    /** 日历图标*/
    UIButton * TimeButton = [[UIButton alloc] initWithFrame:CGRectMake(30 * px, 30 * px, 60 * px, 60 * px)];
    //    [TimeButton setImage:[UIImage imageNamed:@"calendar icon"] forState:UIControlStateNormal];
    [TimeButton setBackgroundImage:[UIImage imageNamed:@"calendar icon"] forState:UIControlStateNormal];
    TimeButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    //    TimeButton.backgroundColor = [UIColor redColor];showView
    [TimeButton addTarget:self action:@selector(showView) forControlEvents:UIControlEventTouchUpInside];
    [timeView addSubview:TimeButton];
    
    /** 左按钮*/
    UIButton *  leftButton= [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(TimeButton.frame) + 40 * px, TimeButton.y, 40 * px, TimeButton.height)];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"日期选择-左箭头"] forState:UIControlStateNormal];
    //        [leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [timeView addSubview:leftButton];
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM"];
    NSDate *datenow = [NSDate date];
    
    NSDate *newDate = [NSDate date];
    
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSMonthCalendarUnit startDate:&beginDate interval:&interval forDate:newDate];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return;
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *beginString = [myDateFormatter stringFromDate:beginDate];
    NSString *endString = [myDateFormatter stringFromDate:endDate];
    
    self.timeStr = [NSString stringWithFormat:@"%@,%@",beginString,endString];
    
    /** 时间*/
    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftButton.frame) + 40 * px, TimeButton.y, timeView.width / 2, TimeButton.height)];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.text = [NSString stringWithFormat:@"%@",[formatter stringFromDate:datenow]];
    timeLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
    [timeView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    /** 右按钮*/
    UIButton *  rightButton= [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame) + 40 * px, TimeButton.y, 40 * px, TimeButton.height)];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"日期选择-右箭头"] forState:UIControlStateNormal];
    //        [rightButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [timeView addSubview:rightButton];
    
    
   
    
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(timeView.frame) + 30 * px, ScreenWidth, 120 * px)];
    titleView.backgroundColor = BackViewColor;
    [HeaderView addSubview:titleView];
    NSArray * titleArray = @[@"头像",@"姓名",@"考勤天数/总天数",@"状态"];
    
    CGFloat CommoditytitleWidth = titleView.width / 5;
    CGFloat CommoditytitleY = 0;
    CGFloat CommoditytitleHeight = titleView.height;
    for (int index = 0; index < titleArray.count; index ++) {
        /** 标题label*/
        UILabel * Commoditytitle = [[UILabel alloc] init];
        Commoditytitle.x = CommoditytitleWidth * index;
        Commoditytitle.y = CommoditytitleY;
        Commoditytitle.width = CommoditytitleWidth;
        Commoditytitle.height = CommoditytitleHeight;
        Commoditytitle.textColor = WYCRGBColor(102, 121, 145);
        Commoditytitle.text = titleArray[index];
        Commoditytitle.textAlignment = NSTextAlignmentCenter;
        Commoditytitle.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [titleView addSubview:Commoditytitle];
        if (index == 2) {
            Commoditytitle.width = CommoditytitleWidth * 2;
        }
        
        if (index == 3) {
            Commoditytitle.x = CommoditytitleWidth * 4;
        }
    }
    
    self.tableView.tableHeaderView = HeaderView;
}

- (void)showView{
    
    [PickerView showPickerView:[UIApplication sharedApplication].keyWindow componentNum:2 typePickerID:2 selectStr:nil StrBlock:^(NSString *string) {
        self.timeLabel.text = [NSString stringWithFormat:@"%@",string];
        
        //        WYCLog(@"string----------%@",string);
        
        NSDateFormatter *format=[[NSDateFormatter alloc] init];
        [format setDateFormat:@"yyyy-MM"];
        NSDate *newDate=[format dateFromString:string];
        double interval = 0;
        NSDate *beginDate = nil;
        NSDate *endDate = nil;
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        [calendar setFirstWeekday:2];//设定周一为周首日
        BOOL ok = [calendar rangeOfUnit:NSMonthCalendarUnit startDate:&beginDate interval:&interval forDate:newDate];
        //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
        if (ok) {
            endDate = [beginDate dateByAddingTimeInterval:interval-1];
        }
        NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
        [myDateFormatter setDateFormat:@"YYYY-MM-dd"];
        NSString *beginString = [myDateFormatter stringFromDate:beginDate];
        NSString *endString = [myDateFormatter stringFromDate:endDate];
        NSString *s = [NSString stringWithFormat:@"%@,%@",beginString,endString];
        self.timeStr = s;
        //        WYCLog(@"s----------%@",s);
        [self ReloadData];
        
    }];
    
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - [self navBarBottom] - 120 * px - self.tabBarController.tabBar.height - 150 * px - 20) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.departmentlistArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MSMonthTongjiCell * cell = [MSMonthTongjiCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tongjiModel = self.departmentlistArray[indexPath.row];

    cell.ButtonDelegate = self;
    [cell.stateLabel addTarget:self action:@selector(stateClick) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)ClickuserName:(NSString *)userName uid:(NSString *)uid avatarUrl:(NSString *)avatarUrl attendacneState:(NSString *)attendacneState{
    
    self.userName = userName;
    self.avatarUrl = avatarUrl;
    self.uid = uid;
}
- (void)stateClick{
    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [[UIApplication sharedApplication].keyWindow  addSubview:view];
    self.backView = view;
    
    
    UIView * showView = [[UIView alloc] init];
    showView.frame = CGRectMake(150 * px, ScreenHeight / 3, ScreenWidth - 300 * px, 800 * px);
    showView.backgroundColor = backgroundcolor;
    [view addSubview:showView];
    self.showView = showView;
    
    /** <#name#>*/
    UIButton * closebutton = [[UIButton alloc] initWithFrame:CGRectMake(showView.width - 80 * px, 20 * px, 60 * px, 60 * px)];
    [closebutton setImage:[UIImage imageNamed:@"pop_dis"] forState:UIControlStateNormal];
    [closebutton addTarget:self action:@selector(closebuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [showView addSubview:closebutton];
    
    /** 日历按钮*/
    UIImageView * photoImg = [[UIImageView alloc] initWithFrame:CGRectMake(showView.width / 2 - 100 * px, 100 * px, 200 * px, 200 * px)];
    [photoImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.avatarUrl]] placeholderImage:[UIImage imageNamed:@"我的头像占位"]];
    photoImg.layer.cornerRadius = 100 * px;
    photoImg.layer.masksToBounds = YES;
    photoImg.backgroundColor = [UIColor redColor];
    [showView addSubview:photoImg];
    
    /** 姓名*/
    UILabel * namelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(photoImg.frame) + 50 * px, showView.width, 60 * px)];
    namelabel.textColor = [UIColor whiteColor];
    namelabel.text = [NSString stringWithFormat:@"%@",self.userName];
    namelabel.textAlignment = NSTextAlignmentCenter;
    namelabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [showView addSubview:namelabel];
    
    /** name*/
    UILabel * stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(namelabel.frame) + 70 * px, showView.width, 60 * px)];
    stateLabel.textColor = FontColor;
    stateLabel.text = @"状态:3天连续未签到";
    stateLabel.textAlignment = NSTextAlignmentCenter;
    stateLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [showView addSubview:stateLabel];
    
    
    
    /** 冻结*/
    UIButton * dongjieButton = [[UIButton alloc] initWithFrame:CGRectMake(50 * px, CGRectGetMaxY(stateLabel.frame) + 100 * px, showView.width / 2 - 100 * px, 80 * px)];
    [dongjieButton setTitle:@"确认冻结" forState:UIControlStateNormal];
    dongjieButton.layer.cornerRadius = 40 * px;
    dongjieButton.layer.masksToBounds = YES;
    dongjieButton.layer.borderColor = FontColor.CGColor;
    dongjieButton.layer.borderWidth = 1;
    dongjieButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [dongjieButton  setTitleColor:FontColor forState:UIControlStateNormal];
    [dongjieButton  addTarget:self action:@selector(dongjie1ButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [showView  addSubview:dongjieButton];
    
    
    /** 离职*/
    UIButton * lizhiButton = [[UIButton alloc] initWithFrame:CGRectMake(showView.width / 2 + 50 * px, dongjieButton.y, dongjieButton.width, dongjieButton.height)];
    [lizhiButton setTitle:@"确认离职" forState:UIControlStateNormal];
    lizhiButton.layer.cornerRadius = 40 * px;
    lizhiButton.layer.masksToBounds = YES;
    [lizhiButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
    lizhiButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [lizhiButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [lizhiButton  addTarget:self action:@selector(lizhiButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [showView  addSubview:lizhiButton];
    
}
- (void)closebuttonClick{
    
    [self.backView removeFromSuperview];
}

- (void)dongjieButtonClick{
    
    MSDongjieController * dongjieVC = [[MSDongjieController alloc] init];
    [self.navigationController pushViewController:dongjieVC animated:NO];
}
- (void)dongjie1ButtonClick{
    
    NSDictionary * params = @{@"userId":self.uid};
    [HTTPRequest POST:MSupdateFrozenState params:params succeed:^(id responseObject) {
        if (CodeStr) {
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self closebuttonClick];
            //            [self ReloadData];
            MSDongjieController * dongjieVC = [[MSDongjieController alloc] init];
            [self.navigationController pushViewController:dongjieVC animated:NO];
        }else{
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self closebuttonClick];
        }
    } failure:^(NSError *error) {
        [self closebuttonClick];
        [MBManager showBriefAlert:Failed];
    }];
    WYCLogFunc;
    MSDongjieController * dongjieVC = [[MSDongjieController alloc] init];
    [self.navigationController pushViewController:dongjieVC animated:NO];
    
}

- (void)lizhiButtonClick{
    
    MSLizhiListController * lizhi = [[MSLizhiListController alloc] init];
    [self.navigationController pushViewController:lizhi animated:NO];
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"考勤统计";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
